package com.cqeec.gams.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cqeec.gams.entity.FunctionalModule;
import com.cqeec.gams.service.FunctionalModuleService;

@Controller
@RequestMapping("/app/function/")
public class FunctionalModuleController {
	@Autowired
	private FunctionalModuleService fmService;

	@RequestMapping("/module/update.html")
	public ModelAndView updateFunctionModule(FunctionalModule fm) {
		ModelAndView mav = new ModelAndView("function/module/edit");
		String parent = fm.getParent();
		mav.addObject("fm", fm);
		// 查询功能组
		mav.addObject("fgList", fmService.getAllFunctionGroup());
		if (parent == null || "".equals(parent)) {
			mav.addObject("msgParent", "请选择功能组！");
			return mav;
		}
		String name = fm.getName();
		if (name == null || "".equals(name)) {
			mav.addObject("msgName", "请输入功能模块名称！");
			return mav;
		}
		String code = fm.getCode();
		if (code == null || "".equals(code)) {
			mav.addObject("msgCode", "请输入功能模块的代码！");
			return mav;
		}
		try {
			fmService.update(fm);
			mav.addObject("message", "保存数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("message", "保存数据失败！");
		}
		return mav;
	}

	@RequestMapping("/module/edit.html")
	public ModelAndView gotoFunctionModuleEditPage(String id) {
		ModelAndView mav = new ModelAndView("function/module/edit");
		// 根据功能模块ID查询功能模块
		mav.addObject("fm", fmService.findById(id));
		// 查询功能组
		mav.addObject("fgList", fmService.getAllFunctionGroup());
		return mav;
	}

	@RequestMapping("/group/update.html")
	public ModelAndView updateFunctionGroup(FunctionalModule fg) {
		ModelAndView mav = new ModelAndView("function/group/edit");
		mav.addObject("fg", fg);
		String name = fg.getName();
		if (name == null || "".equals(name)) {
			mav.addObject("msgName", "请输入功能组名称！");
			return mav;
		}
		try {
			fmService.update(fg);
			mav.addObject("message", "保存数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("message", "保存数据失败！");
		}
		return mav;
	}

	@RequestMapping("/group/edit.html")
	public ModelAndView gotoFunctionGroupEditPage(String id) {
		ModelAndView mav = new ModelAndView("function/group/edit");
		// 根据功能组ID查询功能组
		mav.addObject("fg", fmService.findById(id));
		return mav;
	}

	@RequestMapping("/module/save.html")
	public ModelAndView saveFunctionModule(FunctionalModule fm, RedirectAttributes ra) {
		ModelAndView mav = new ModelAndView("function/module/add");
		String parent = fm.getParent();
		if (parent == null || "".equals(parent)) {
			mav.addObject("msgParent", "请选择功能组！");
			return mav;
		}
		String name = fm.getName();
		if (name == null || "".equals(name)) {
			mav.setViewName("function/module/add");
			mav.addObject("msgName", "请输入功能模块名称！");
			return mav;
		}
		String code = fm.getCode();
		if (code == null || "".equals(code)) {
			mav.setViewName("function/module/add");
			mav.addObject("msgName", "请输入功能模块的代码！");
			return mav;
		}
		String id = fm.getId();
		if (id == null || "".equals(id)) {
			fm.setId(UUID.randomUUID().toString());
		}
		try {
			fm.setType("2");
			fmService.insert(fm);
			mav.setViewName("redirect:/app/function/module/add.html");
			ra.addFlashAttribute("message", "保存数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("message", "保存数据失败！");
			mav.setViewName("function/module/add");
		}
		return mav;
	}

	@RequestMapping("/module/add.html")
	public ModelAndView gotoFunctionModuleAddPage() {
		ModelAndView mav = new ModelAndView("function/module/add");
		// 查询功能组
		List<FunctionalModule> fgList = fmService.getAllFunctionGroup();
		mav.addObject("fgList", fgList);
		return mav;
	}

	@RequestMapping("/group/save.html")
	public ModelAndView saveFunctionGroup(FunctionalModule fg, RedirectAttributes ra) {
		ModelAndView mav = new ModelAndView();
		String name = fg.getName();
		if (name == null || "".equals(name)) {
			mav.setViewName("function/group/add");
			mav.addObject("msgName", "请输入功能组名称！");
			return mav;
		}
		String id = fg.getId();
		if (id == null || "".equals(id)) {
			fg.setId(UUID.randomUUID().toString());
		}
		try {
			fg.setType("1");
			fmService.insert(fg);
			mav.setViewName("redirect:/app/function/group/add.html");
			ra.addFlashAttribute("message", "保存数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("message", "保存数据失败！");
			mav.setViewName("function/group/add");
		}
		return mav;
	}

	@RequestMapping("/group/add.html")
	public String gotoFunctionGroupAddPage() {
		return "function/group/add";
	}

	@RequestMapping("/list.html")
	public String gotoListPage() {
		return "function/list";
	}

}
